<style lang="scss">
  .model-list {
    .zk-pagination__total {
      float: right;
    }
    .zk-table__empty-text {
      height: 300px;
    }
  }
</style>
<template>
  <div class="model-list">
    <zk-grid-box
     :scope="this"
     :grid-data="data"
     v-loading.body="loading"
     ref="gridbox"
     @query="queryGrid"
     :columns="columns">
     <zk-table-column
       slot="operate-bar"
       :label="$t('common_op')"
        inline-template prop="id"
       show-on-hover>
        <zk-operate-bar
          @edit="scope.edit(row, true)"
          @delete="scope.del(row, true)"
        ></zk-operate-bar>
      </zk-table-column>
    </zk-grid-box>
  </div>
</template>
<script>
  import * as ${modelName?cap_first}Api from 'src/api/${modelName?uncap_first}'
  export default {
    name: '${vueTab?uncap_first}-list',
    data () {
      return {
        data: [],
        columns: [],
        loading: true,
        searchParams: {
        }
      }
    },
    methods: {
      setParams (v) {
        this.searchParams.filter = v
      },
      search() {
        this.$refs.gridbox.query()
      },
      edit(row) {
        this.$emit('edit', row, true)
      },
      getColumns() {
        let self = this
        let param = {}
        param.className = 'com.zkteco.biosecurity.${moduleName}.vo.${modelName?cap_first}Vo'
        ${modelName?cap_first}Api.getColumns(param).then(data => {
          self.columns = data.body.payload.tableColumns
        })
      },
      queryGrid(pager, grid) {
        this.loading = true
        let param = JSON.parse(JSON.stringify(this.searchParams))
        Object.assign(param, pager)
        ${modelName?cap_first}Api.get(param).then((response) => {
          if (response.body.code === '00000000') {
            this.data = response.body.payload.results
            pager.totalRecords = response.body.payload.totalRecords
          } else {
            this.$message({message: this.$t('common_query_faild'), type: 'error'})
          }
          this.loading = false
        }, () => {
          this.loading = false
          this.$message({message: this.$t('common_query_faild'), type: 'error'})
        })
      },
      del(row) {
        this.$confirm(this.$t('common_deleteWarn'), this.$t('common_tip'), {
          confirmButtonText: this.$t('common_sure'),
          cancelButtonText: this.$t('common_cancel'),
          type: 'warning'
        }).then(() => {
          ${modelName?cap_first}Api.del({ id: row.id }).then((response) => {
            if (response.body.code === '00000000') {
              this.$message({message: this.$t('common_del_success'), type: 'success'})
              this.$refs.gridbox.query()
            } else {
              this.$message({message: response.body.message != null ? response.body.messag : this.$t('common_del_faild'), type: 'error'})
            }
          }, () => {
            this.$message({message: this.$t('common_del_faild'), type: 'error'})
          })
        }, () => {})
      }
    },
    created () {
      console.log('created')
      this.getColumns()
    },
    mounted() {
    }
  }
</script>
